package 电话号码的字母组合;



import java.util.*;

public class Solution {

    private static String[] map = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};

    public static void main(String[] args) {


        String digits = "9";

        System.out.println(map[digits.charAt(0)-'2']);

       /* Solution solution = new Solution();
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        solution.letterCombinations("2");
        stopWatch.stop();
        System.out.println("sw.prettyPrint()~~~~~~~~~~~~~~~~~");
        System.out.println(stopWatch.prettyPrint());*/
    }

    public List<String> letterCombinations(String digits) {
        if(digits.equals(""))
            return new ArrayList<>();

        Map<Character, String> map = new HashMap<Character, String>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};

        List<Integer> sizeList = new ArrayList<>();

        char[] chars = digits.toCharArray();
        int total = 1;
        for (char aChar : chars) {
            int length = map.get(aChar).length();
            sizeList.add(length);

            total = total * length;
        }

        List<StringBuffer> result = new ArrayList<>();
        for (int i = 0; i < total; i++) {
            result.add(new StringBuffer());
        }
        //int count = total;
        //系数
        int mark = 1;

        int count = 0;
        for (int i = 0; i < sizeList.size(); i++) {
            if(i == 0){
                count = 1;
            }
            else{
                count *= sizeList.get(i-1);
            }

            total = total / sizeList.get(i);

            //数字对应的 字符串 比如 2 -> “abc”
            String startStr = map.get(chars[i]);

            char[] charArray = startStr.toCharArray();
            for (int j = 0; j < charArray.length; j++) {
                //当前字符
                char currentChar = charArray[j];

                int tempMark = 0;
                while(tempMark < count) {
                    for (int k = 0; k < total; k++) {
                        int index = j * total + k + (tempMark * mark);
                        StringBuffer str = result.get(index);
                        str.append(currentChar);
                        result.set(index, str);
                    }
                    tempMark++;
                }
            }
            mark = total;
        }

        List<String> list = new ArrayList<>();
        result.forEach(item->{
            if(!item.toString().equals(""))
                list.add(item.toString());
        });

        return list;
    }
}
